package com.golte.dataform.analysis.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.golte.common.GlobalResponse;
import com.golte.dataform.analysis.common.JwtUtil;
import com.golte.dataform.analysis.service.ReportService;
import com.golte.dataform.analysis.vo.request.ReportReq;
import com.golte.dataform.analysis.vo.response.*;
import com.golte.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sm
 * @Date: 2021/3/6
 */
@RestController
@Slf4j
@RequestMapping("/report")
@Api(value = "星选报表", description = "星选报表")
public class ReportController {

    private static final String INDUSTTRY_PREFIX = "FXREPORT:";
    private static final String INDUSTTRY_BRAND = INDUSTTRY_PREFIX + "INDUSTTRY_BRAND";

    @Autowired
    private ReportService reportService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/getToken/{role}")
    @ApiOperation("获取token")
    public GlobalResponse<String> getToken(@PathVariable("role") Integer role, @RequestBody String data) {
        log.info("请求参数：{}", data);
        String token = IdUtil.randomUUID();

        redisUtil.set(INDUSTTRY_PREFIX + token, data, 7200);
        redisUtil.set(INDUSTTRY_PREFIX + token + ":role", role, 7200);
//        return GlobalResponse.ok(jwtUtil.createToken(data));
        return GlobalResponse.ok(token);

    }

//    @PostMapping("/getIndustry")
//    @ApiOperation("获取产品品牌")
//    public GlobalResponse<String> getIndustry(String token) {
//        log.info("请求参数：{}",token);
//        return GlobalResponse.ok(jwtUtil.getDataByToken(token));
//    }

    @GetMapping("/getIndustry")
    @ApiOperation("获取产品品牌")
    public GlobalResponse getIndustry(String token) {
        log.info("请求参数：{}", token);
        Integer role = (Integer) redisUtil.get(INDUSTTRY_PREFIX + token + ":role");
        Object data = null;
        if (1 == role) {
            data = getIndustryAndBrand();
        } else {
            data = redisUtil.get(INDUSTTRY_PREFIX + token);
        }


//            return  data.toString();
        return GlobalResponse.ok(JSON.parseArray(data.toString()));
//        return jwtUtil.getDataByToken(token);
    }

//    @PostMapping("/getIndustry")
//    @ApiOperation("获取产品品牌")
//    public GlobalResponse<List<Industy>> getIndustry(String token) {
//        log.info("请求参数：{}",token);
//        return GlobalResponse.ok(jwtUtils.getDataByToken(token));
//    }

    @GetMapping("/overview")
    @ApiOperation("头部概况")
    public GlobalResponse<ReportOverviewResp> getOverview(ReportReq req) {
        log.info("请求参数：{}", req);
        return GlobalResponse.ok(reportService.getOverview(req));

    }

    @GetMapping("/userOverview")
    @ApiOperation("会员概况")
    public GlobalResponse<UserOverviewResp> getUserOverview(ReportReq req) {
        log.info("请求参数：{}", req);
        return GlobalResponse.ok(reportService.getUserOverview(req));

    }

    @GetMapping("/userChannelDistribution")
    @ApiOperation("会员来源渠道分布")
    public GlobalResponse<List<UserChannelDistributionResp>> getUserChannelDistribution(ReportReq req) {
        log.info("请求参数：{}", req);
        return GlobalResponse.ok(reportService.getUserChannelDistribution(req));
    }

    @GetMapping("/rights")
    @ApiOperation("权益概况")
    public GlobalResponse<RightsOverviewResp> getRightsOverview(ReportReq req) {
        log.info("请求参数：{}", req);
        return GlobalResponse.ok(reportService.getRightsOverview(req));
    }

    @GetMapping("/rights/group/top5")
    @ApiOperation("权益类型TOP5")
    public GlobalResponse<RightsTop5Resp> getRightsGroupTop5(ReportReq req) {
        log.info("请求参数：{}", req);
        return GlobalResponse.ok(reportService.getRightsGroupTop5(req));
    }

    @GetMapping("/rights/top5")
    @ApiOperation("权益TOP5")
    public GlobalResponse<List<RightsTop5>> getRightsTop5(ReportReq req) {
        log.info("请求参数：{}", req);
        return GlobalResponse.ok(reportService.getRightsTop5(req));
    }

    @GetMapping("/industry")
    @ApiOperation("产业概况")
    public GlobalResponse<IndustryOverviewResp> getIndustryOverview(ReportReq req) {
        log.info("请求参数：{}", req);
        return GlobalResponse.ok(reportService.getIndustryOverview(req));
    }


    public String getIndustryAndBrand() {
        Object data = redisUtil.get(INDUSTTRY_BRAND);
        if (null == data) {
            List<Industy> list = reportService.getAllIndustryAndBrand();
            data = JSON.toJSONString(list);
            redisUtil.set(INDUSTTRY_BRAND, data, 3600);
        }
        return data.toString();

    }


}
